package cn.edu.zjut;

import com.mathworks.toolbox.javabuilder.MWArray;
import com.mathworks.toolbox.javabuilder.MWClassID;
import com.mathworks.toolbox.javabuilder.MWComplexity;
import com.mathworks.toolbox.javabuilder.MWNumericArray;
import drawplot.Plotter;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.time.ZoneId;
import java.util.*;


/**
 * @ClassName: UKFDemo
 * @Description:
 * @Author: 12071
 * @Date: 2022/1/22 18:05
 */
public class FilterDemo {
    public static List<NodePosition> positionsList = new ArrayList<>();

    public static void main(String[] args) {

        try {
            //创建工作簿对象
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(".\\861714059532774-16-42-原(1).xlsx"));
            //获取工作簿下sheet的个数
            int sheetNum = xssfWorkbook.getNumberOfSheets();
            System.out.println("该excel文件中总共有：" + sheetNum + "个sheet");
            //获取第一个sheet
            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
            //获取最后一行的编号（即总行数,下标从0开始）
            int maxRow = sheet.getLastRowNum();
            System.out.println("读取第1个sheet: " + sheet.getSheetName() + "，共有" + (maxRow + 1) + "行");
            for (int i = 0; i <= maxRow; i++) {
                XSSFRow row = sheet.getRow(i);
                int maxCel = row.getLastCellNum();
                double longitude = row.getCell(2).getNumericCellValue();
                double altitude = row.getCell(3).getNumericCellValue();
                long updateTime = row.getCell(13).getLocalDateTimeCellValue().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
                positionsList.add(new NodePosition("532576", longitude, altitude, updateTime));
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        System.out.println(positionsList.toString());
        int size = positionsList.size();


        /* 以下为调用自定义MATLAB函数drawplot2Line()展示效果 */
        FilterWindow window = new FilterWindow("532774", 5L);
        for (NodePosition position : positionsList) {
            window.insertPositionWithMovingMean(position);
        }
        System.out.println(positionsList.toString());
        System.out.println(window.getPositionHistory());

        MWNumericArray x0=null;
        MWNumericArray y0=null;
        MWNumericArray x=null;
        MWNumericArray y=null;

        Plotter plotter = null;
        try{
            int[] dims = {1, size};
            x0 = MWNumericArray.newInstance(dims, MWClassID.DOUBLE, MWComplexity.REAL);
            y0 = MWNumericArray.newInstance(dims, MWClassID.DOUBLE, MWComplexity.REAL);
            x = MWNumericArray.newInstance(dims, MWClassID.DOUBLE, MWComplexity.REAL);
            y = MWNumericArray.newInstance(dims, MWClassID.DOUBLE, MWComplexity.REAL);
            for (int i = 1; i <= size; i++) {
                x0.set(i,window.getPositionHistory().get(i-1).getXOri());
                y0.set(i,window.getPositionHistory().get(i-1).getYOri());
                x.set(i,window.getPositionHistory().get(i-1).getX());
                y.set(i,window.getPositionHistory().get(i-1).getY());
            }

            plotter = new Plotter();
            plotter.drawplot2Line(x0,y0,x,y);
            plotter.waitForFigures();
        }catch (Exception e){
            System.out.println(e);
        }finally {
            // 释放本地资源
            MWArray.disposeArray(x0);
            MWArray.disposeArray(y0);
            MWArray.disposeArray(x);
            MWArray.disposeArray(y);
            if (plotter != null) {
                plotter.dispose();
                System.out.println("over");
            }
        }
    }


}
